package com.zenglx.multilanguage.method;


import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import com.zenglx.multilanguage.metadata.ZlxTableInfo;
import com.zenglx.multilanguage.util.SqlHelper;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * SelectList 兼容MP原生方法
 */
public class SelectBatchByIds extends com.baomidou.mybatisplus.core.injector.methods.SelectBatchByIds {

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        ZlxTableInfo zlxTableInfo = (ZlxTableInfo)tableInfo;
        if(zlxTableInfo.isMultiLanguage()) {
            SqlMethod sqlMethod = SqlMethod.SELECT_BATCH_BY_IDS;
            SqlSource sqlSource = languageDriver.createSqlSource(configuration, String.format(sqlMethod.getSql(),
                    SqlHelper.getAllColumns(tableInfo), SqlHelper.selectFromTableTl(tableInfo), tableInfo.getKeyColumn(),
                    SqlScriptUtils.convertForeach("#{item}", COLLECTION, null, "item", COMMA),
                    tableInfo.getLogicDeleteSql(true, true)), Object.class);
            return addSelectMappedStatementForTable(mapperClass, getMethod(sqlMethod), sqlSource, tableInfo);
        } else {
            return super.injectMappedStatement(mapperClass, modelClass, tableInfo);
        }
    }
}
